<!--- Role params --->
<cfparam name="organizationID" type="integer" default="0">
<cfparam name="regionID" type="integer" default="0">
<!--- Sorting params --->
<cfparam name="orderBy" type="string" default="#client.fundsOrderBy#">
<cfparam name="orderDir" type="string" default="#client.fundsOrderDir#">
<!--- Filter params --->
<cfparam name="filterOrganizationIDs" type="string" default="#client.fundsOrganizationIDs#">
<cfparam name="filterRegionIDs" type="string" default="#client.fundsRegionIDs#">
<cfparam name="filterPBS" type="string" default="#client.fundsPBS#">
<cfparam name="filterApproved" type="string" default="#client.fundsApproved#">
<cfparam name="filterPeriod" type="string" default="#client.fundsPeriod#">
<cfparam name="filterYear" type="string" default="#client.fundsYear#">
<!--- Checkboxes --->
<cfparam name="fundIDs" type="string" default="">

<cfset funds = application.fundGateway.getAllAsQuery("*", organizationID, regionID, orderBy, orderDir, filterOrganizationIDs, filterRegionIDs, filterPBS, filterApproved, 0, filterPeriod, filterYear)>

<cfoutput>

	<cfform name="fundsForm" id="fundsForm" action="#script_name#" method="post">
		<cfinput type="hidden" name="view" value="#view#">

		<div class="buttonBar">
			<table cellpadding="0" cellspacing="0" border="0" width="100%">
				<tr>
					<td>
						<cfif IsUserInRole("Organizer") or IsUserInRole("Super User")>
							<a href="#script_name#?view=createFund&rv=#view#&fundID=0&organizationID=#organizationID#" class="formLink">New&nbsp;request</a>
						</cfif>
						<cfif (IsUserInRole("Super User") or IsUserInRole("Regional Admin")) and funds.RecordCount gt 0>
							<a href="" onclick="emailSelected(); return false;" class="formLink">E-mail selected</a>
							<a href="/applications/admin/methods/export.cfm?view=#view#&organizationID=#organizationID#&regionID=#regionID#" target="_blank" class="formLink">Export</a>
							<cfif IsUserInRole("Super User")>
								<a href="#script_name#?view=reportFundSurveys&rv=#view#&organizationID=#organizationID#&regionID=#regionID#" class="formLink">Report</a>
							</cfif>
						</cfif>
					</td>
					<td align="right">
						<strong>Count:</strong> #funds.RecordCount#
					</td>
				</tr>
			</table>
		</div>

		<!--- filter --->
		<cfif IsUserInRole("Super User") or IsUserInRole("Regional Admin")>
			<script type="text/javascript" language="javascript">
			<!--
				toggleVisibility = function(objId) {
					var obj = document.getElementById(objId);
					if (obj.style.visibility == "visible") {
						obj.style.visibility = "hidden";
						obj.style.display = "none";
					} else {
						obj.style.visibility = "visible";
						obj.style.display = "block";
					}
				}

				updateFilterLink = function() {
					if (document.getElementById("filterSelections").style.visibility == "visible") {
						document.getElementById("filterLink").innerHTML = '#minusImage#&nbsp;Hide Filter';
					} else {
						document.getElementById("filterLink").innerHTML = '#plusImage#&nbsp;Show Filter';
					}
				}

				updateIDsSelect = function(selectObj) {
					if (selectObj.selectedIndex == 0) {
						selectObj.selectedIndex = 0;
					}
				}

				updateTimePeriod = function(radioObj) {
					if ($(radioObj).val() == "Year") {
						$("##filterYear").show();
					} else {
						$("##filterYear").hide();
					}
					$("##filterYear").attr("selectedIndex", 0);
				}

			// -->
			</script>

			<cfset organizations = application.organizationGateway.getAllAsQuery("Organization_ID,Organization", organizationID, regionID, "Organization")>
			<cfset regions = application.countyGateway.getRegions()>

			<table class="filter" width="100%">
				<tr>
					<td class="filterHeader"><a id="filterLink"href="" onclick="toggleVisibility('filterSelections'); toggleVisibility('filterButtons'); updateFilterLink(); return false;" style="width: 100px; display: inline-block;">#plusImage#&nbsp;Show Filter</a><cfif filterOrganizationIDs neq 0 or filterRegionIDs neq 0 or filterApproved neq "All" or filterPeriod neq "Year" or filterPBS neq application.DEFAULT_PBS or filterYear neq request.programYears.current.fund>List is filtered</cfif></td>
				</tr>
				<tr id="filterSelections" style="visibility: hidden; display: none;">
					<td>

						<table>
							<tr>
								<td>
								<fieldset>
									<legend>Organizations</legend>
									<cfselect name="filterOrganizationIDs" size="7" multiple="true" required="yes" message="Organization filter is required" onChange="updateIDsSelect(this);">
										<option value="0"<cfif filterOrganizationIDs eq 0> selected</cfif>>All</option>
										<cfloop query="organizations">
											<option value="#Organization_ID#"<cfif ListFind(filterOrganizationIDs, Organization_ID)> selected</cfif>>#Organization#</option>
										</cfloop>
									</cfselect>
									<div class="footnote">Ctrl to multi-select</div>
								</fieldset>
								</td>
								<cfif IsUserInRole("Super User")>
									<td>
										<fieldset>
											<legend>Regions</legend>
											<cfselect name="filterRegionIDs" size="7" multiple="true" required="yes" message="Region filter is required" onChange="updateIDsSelect(this);" style="width: 80px;">
												<option value="0"<cfif filterRegionIDs eq 0> selected</cfif>>All</option>
												<cfloop query="regions">
													<option value="#Region_ID#"<cfif ListFind(filterRegionIDs, Region_ID)> selected</cfif>>#Region_ID#</option>
												</cfloop>
											</cfselect>
											<div class="footnote">Ctrl to multi-select</div>
										</fieldset>
									</td>
									<td>
										<fieldset>
											<legend>PBS</legend>
											<input type="radio" id="pbs1" name="filterPBS" value="All"<cfif filterPBS eq "All"> checked="true"</cfif>><label for="pbs1">&nbsp;All</label><br>
											<input type="radio" id="pbs2" name="filterPBS" value="Yes"<cfif filterPBS eq "Yes"> checked="true"</cfif>><label for="pbs2">&nbsp;PBS</label><br>
											<input type="radio" id="pbs3" name="filterPBS" value="No"<cfif filterPBS eq "No"> checked="true"</cfif>><label for="pbs3">&nbsp;Non-PBS</label>
										</fieldset>
									</td>
								</cfif>
								<td>
									<fieldset>
										<legend>Status</legend>
										<input type="radio" id="approved1" name="filterApproved" value="All"<cfif filterApproved eq "All"> checked="true"</cfif>><label for="approved1">&nbsp;All</label><br>
										<input type="radio" id="approved2" name="filterApproved" value="Yes"<cfif filterApproved eq "Yes"> checked="true"</cfif>><label for="approved2">&nbsp;Approved</label><br>
										<input type="radio" id="approved3" name="filterApproved" value="No"<cfif filterApproved eq "No"> checked="true"</cfif>><label for="approved3">&nbsp;Not approved</label>
									</fieldset>
								</td>
								<td>
									<fieldset>
										<legend>Request Date</legend>
										<input type="radio" id="Period1" name="filterPeriod" value="All" onclick="updateTimePeriod(this);"<cfif filterPeriod eq "All"> checked="true"</cfif>><label for="Period1">&nbsp;All Program Years</label><br>
										<input type="radio" id="Period2" name="filterPeriod" value="Year" onclick="updateTimePeriod(this);"<cfif filterPeriod eq "Year"> checked="true"</cfif>><label for="Period2">&nbsp;Program Year</label>
										<select name="filterYear" id="filterYear" size="1"<cfif filterPeriod neq "Year"> style="display: none;"</cfif>>
											<cfloop list="#listSort(structKeyList(request.programYears.fund), "text")#" index="i">
												<option value="#i#"<cfif filterYear eq i> selected</cfif>>#i#</option>
											</cfloop>
										</select>
									</fieldset>
								</td>
								<td width="100%"></td>
							</tr>
						</table>

					</td>
				</tr>
				<tr id="filterButtons" style="visibility: hidden; display: none;">
					<td>
						<div style="margin-bottom: 4px;">
							<cfinput type="submit" name="applyFilterButton" id="applyFilterButton" value="Apply Filter" class="formButton2">
							<cfif filterOrganizationIDs neq 0 or filterRegionIDs neq 0 or filterApproved neq "All" or filterPeriod neq "Year" or filterPBS neq application.DEFAULT_PBS or filterYear neq request.programYears.current.fund>
								<a href="#script_name#?view=#view#&filterOrganizationIDs=0&filterRegionIDs=0&filterApproved=All&filterPeriod=Year&filterYear=#request.programYears.current.fund#&filterPBS=#application.DEFAULT_PBS#" class="formLink2">Clear filter</a>
							</cfif>
						</div>
					</td>
				</tr>
			</table>
		</cfif>

		<cfif funds.RecordCount gt 0>

			<!--- checkbox functions --->
			<cfif IsUserInRole("Super User") or IsUserInRole("Regional Admin")>
				<script type="text/javascript" language="javascript">
				<!--
					toggleSelectAll = function(f) {
						var c = f.selectAllCheckbox;
						for (var i=0; i<f.fundIDs.length; i++) {
							if (c.checked) {
								f.fundIDs[i].checked = true;
							} else {
								f.fundIDs[i].checked = false;
							}
						}
					}

					updateSelectAll = function(chk) {
						if (!chk) {
							document.getElementById("selectAllCheckbox").checked = false;
						}
					}

					emailSelected = function() {
						var f = document.getElementById("fundsForm");
						var fundIDs = new Array();
						for (var i=0; i<f.fundIDs.length; i++) {
							if (f.fundIDs[i].checked) {
								fundIDs[fundIDs.length] = f.fundIDs[i].value;
							}
						}
						if (fundIDs.length == 0) {
							alert("Select at least one fund");
						} else {
							eSF = document.getElementById("emailSelectedForm");
							eSF.fundIDs.value = fundIDs;
							eSF.submit();
						}
					}
				// -->
				</script>
			</cfif>

			<table class="list" width="100%">
				<tr>
					<cfif IsUserInRole("Super User") or IsUserInRole("Regional Admin")>
						<th><input name="selectAllCheckbox" id="selectAllCheckbox" type="checkbox" onclick="toggleSelectAll(this.form);"></th>
					</cfif>
					<th><a href="#script_name#?view=#view#&orderBy=Fund_ID<cfif orderBy eq 'Fund_ID' and orderDir eq 'asc'>&orderDir=desc</cfif>">ID<cfif orderBy eq "Fund_ID">&nbsp;&nbsp;<cfif orderDir eq "asc">#ascImage#<cfelse>#descImage#</cfif></cfif></a></th>
					<cfif IsUserInRole("Super User") or IsUserInRole("Regional Admin")>
						<th><a href="#script_name#?view=#view#&orderBy=Organization<cfif orderBy eq 'Organization' and orderDir eq 'asc'>&orderDir=desc</cfif>">Organization<cfif orderBy eq "Organization">&nbsp;&nbsp;<cfif orderDir eq "asc">#ascImage#<cfelse>#descImage#</cfif></cfif></a></th>
						<th><a href="#script_name#?view=#view#&orderBy=Region_ID<cfif orderBy eq 'Region_ID' and orderDir eq 'asc'>&orderDir=desc</cfif>">Region<cfif orderBy eq "Region_ID">&nbsp;&nbsp;<cfif orderDir eq "asc">#ascImage#<cfelse>#descImage#</cfif></cfif></a></th>
					</cfif>
					<th><a href="#script_name#?view=#view#&orderBy=fundDate<cfif orderBy eq 'fundDate' and orderDir eq 'asc'>&orderDir=desc</cfif>">Request Date<cfif orderBy eq "fundDate">&nbsp;&nbsp;<cfif orderDir eq "asc">#ascImage#<cfelse>#descImage#</cfif></cfif></a></th>
					<th><a href="#script_name#?view=#view#&orderBy=RequestedBy.Last_Name<cfif orderBy eq 'RequestedBy.Last_Name' and orderDir eq 'asc'>&orderDir=desc</cfif>">Requested By<cfif orderBy eq "RequestedBy.Last_Name">&nbsp;&nbsp;<cfif orderDir eq "asc">#ascImage#<cfelse>#descImage#</cfif></cfif></a></th>
					<cfif regionID neq 12>
						<th><a href="#script_name#?view=#view#&orderBy=Mailing_Last_Name<cfif orderBy eq 'Mailing_Last_Name' and orderDir eq 'asc'>&orderDir=desc</cfif>">Mail To<cfif orderBy eq "Mailing_Last_Name">&nbsp;&nbsp;<cfif orderDir eq "asc">#ascImage#<cfelse>#descImage#</cfif></cfif></a></th>
					</cfif>
					<th><a href="#script_name#?view=#view#&orderBy=workshopCount<cfif orderBy eq 'workshopCount' and orderDir eq 'asc'>&orderDir=desc</cfif>">Workshops<cfif orderBy eq "workshopCount">&nbsp;&nbsp;<cfif orderDir eq "asc">#ascImage#<cfelse>#descImage#</cfif></cfif></a></th>
					<cfif IsUserInRole("Super User") or IsUserInRole("Regional Admin")>
						<th><a href="#script_name#?view=#view#&orderBy=qworkshopCount<cfif orderBy eq 'qworkshopCount' and orderDir eq 'asc'>&orderDir=desc</cfif>">Qualifying Workshops<cfif orderBy eq "qworkshopCount">&nbsp;&nbsp;<cfif orderDir eq "asc">#ascImage#<cfelse>#descImage#</cfif></cfif></a></th>
						<th><a href="#script_name#?view=#view#&orderBy=fundAmount<cfif orderBy eq 'fundAmount' and orderDir eq 'asc'>&orderDir=desc</cfif>">Amount Funded<cfif orderBy eq "fundAmount">&nbsp;&nbsp;<cfif orderDir eq "asc">#ascImage#<cfelse>#descImage#</cfif></cfif></a></th>
					</cfif>
					<th><a href="#script_name#?view=#view#&orderBy=fundApproved<cfif orderBy eq 'fundApproved' and orderDir eq 'asc'>&orderDir=desc</cfif>">Status<cfif orderBy eq "fundApproved">&nbsp;&nbsp;<cfif orderDir eq "asc">#ascImage#<cfelse>#descImage#</cfif></cfif></a></th>
					<cfif IsUserInRole("Super User") or IsUserInRole("Regional Admin")>
						<th><a href="#script_name#?view=#view#&orderBy=fundApprovedDate<cfif orderBy eq 'fundApprovedDate' and orderDir eq 'asc'>&orderDir=desc</cfif>">Approval Date<cfif orderBy eq "fundApprovedDate">&nbsp;&nbsp;<cfif orderDir eq "asc">#ascImage#<cfelse>#descImage#</cfif></cfif></a></th>
					</cfif>
				</tr>

				<cfloop query="funds">
					<tr<cfif currentRow mod 2 eq 0> class="evenRow"<cfelse> class="oddRow"</cfif>>
						<cfif IsUserInRole("Super User") or IsUserInRole("Regional Admin")>
							<td><input type="checkbox" name="fundIDs" value="#Fund_ID#" onclick="updateSelectAll(this.checked);"<cfif ListFind(fundIDs, Fund_ID)> checked</cfif>></td>
						</cfif>
						<cfif IsUserInRole("Super User") or IsUserInRole("Regional Admin") or IsUserInRole("Organizer")>
							<td><a href="#script_name#?view=fund&rv=#view#&fundID=#Fund_ID#&organizationID=#Organization_ID#">#Fund_ID#</a></td>
						<cfelse>
							<td>#Fund_ID#</td>
						</cfif>
						<cfif IsUserInRole("Super User") or IsUserInRole("Regional Admin")>
							<td>
								<cfif organizationApproved eq 1>
									#Organization#
								<cfelse>
									<font color="red">#Organization#</font>
								</cfif>
							</td>
							<td>#Region_ID#<cfif study_region>&nbsp;(PBS)</cfif></td>
						</cfif>
						<td>#DateFormat(fundDate, "m/d/yyyy")#</td>
						<td>
							<cfif NOT requestedByApproved>
								<font color="red">#First_Name# #Last_Name#</font>
							<cfelse>
								#First_Name# #Last_Name#
							</cfif>
						</td>
						<cfif regionID neq 12>
							<td>#Mailing_First_Name# #Mailing_Last_Name#</td>
						</cfif>
						<td>#workshopCount#</td>
						<cfif IsUserInRole("Super User") or IsUserInRole("Regional Admin")>
							<td>#qworkshopCount#</td>
							<td>
								<cfif IsNumeric(fundAmount)>
									#DollarFormat(fundAmount)#
								</cfif>
							</td>
						</cfif>
						<td>
							<cfif fundApproved>
								Approved
							<cfelse>
								<cfif IsUserInRole("Super User") or IsUserInRole("Regional Admin")>
									<font color="red">Not approved</font>
								<cfelse>
									Pending approval
								</cfif>
							</cfif>
						</td>
						<cfif IsUserInRole("Super User") or IsUserInRole("Regional Admin")>
							<td>
								<cfif fundApproved and isDate(fundApprovedDate)>
									#dateFormat(fundApprovedDate, "m/d/yyyy")#
								</cfif>
							</td>
						</cfif>
					</tr>
				</cfloop>
			</table>
		</cfif>

	</cfform>

	<cfif funds.RecordCount eq 0>
		<div class="alert alert-info notFound">No funds with the selected filter criteria</div>
	</cfif>

</cfoutput>